home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9849 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.7 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Segmentation Fault ???
  5. Date: 11 Mar 96 12:15:17 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.826546517@rscernix>
  8. References: <4hsa7i$en3@wraith.its.uow.edu.au> <4huis1$cm2@ccshst05.cs.uoguelph.ca> <4hv75jINNpss@keats.ugrad.cs.ubc.ca> <4hvaq3$n8f@ccshst05.cs.uoguelph.ca>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4hvaq3$n8f@ccshst05.cs.uoguelph.ca> thay@uoguelph.ca (Toby K Hay) writes:
  13.  
  14. >Kazimir Kylheku (c2a192@ugrad.cs.ubc.ca) wrote:
  15. >
  16. >: It means that you are making errors in your code that the Turbo C environment
  17. >: doesn't catch. The bus error is likely caused by invoking
  18. >: implementation-specific behavior that is in contravention to standard C:
  19. >: converting a pointer to one that has a stricter alignment. On many of the
  20. >: processors used in UNIX workstations, the address of a long word has to be
  21. >: divisible by four. On a 68000 processor, the address of a 16-bit word has to be
  22. >: divisible by two.
  23. >: If you fail to meet these alignment restrictions, the hardware will trigger an
  24. >: exception, and the UNIX kernel will send a SIGBUS signal to your program.
  25. >
  26. >Would lint catch this for me,
  27.  
  28. Maybe, maybe not.
  29.  
  30. >or will I have to learn about alignment restrictions to run my program?
  31.  
  32. You don't have to learn anything about alignment restrictions.  If your
  33. code is correctly written, the alignment restrictions are taken care of
  34. by the compiler (which knows everything about alignment restrictions).
  35.  
  36. If you convert a pointer to a data with looser alignment restrictions
  37. to a pointer to data with stricter alignment restrictions, you're shooting
  38. yourself in the foot.  Actually, you're shooting youself in the foot with
  39. almost all pointer conversions, the only one which is safe is to signed
  40. or unsigned char pointer (the compiler will perform conversions to and
  41. from void pointers automatically for you, except for some special cases:
  42. functions without prototypes or variable argument lists).
  43.  
  44. >: A ``segmentation fault'', (SIGSEGV signal), on the other hand, is caused by
  45. >: accessing illegal memory, such as dereferencing a null pointer, reaching past
  46. >: the limits of your malloc heap or stack and so forth.
  47. >
  48. >From three replies I received via E-mail I understand that accessing 
  49. >illegal memory is almost certainly caused by using uninitialized pointers 
  50. >- something I will check my code for again. 
  51.  
  52. Some compilers can do this checking for you, if you know how to ask them to
  53. (e.g. gcc -O -Wall).  However, "almost certainly" is a bit of exaggeration:
  54. badly initialized pointers (e.g. explicitly or implicitly with NULL),
  55. bad pointer arithmetic and bad free() calls are also frequent causes of
  56. segmentation faults.
  57.  
  58. >But what determines the 
  59. >limits of my malloc heap, stack, and so forth?  Can I request more memory 
  60. >for these at the command line when I start the program?
  61.  
  62. malloc will tell you when it failed to allocate the requested memory block.
  63. You should NEVER use the value returned by malloc before testing it.
  64.  
  65. If you have a stack overflow problem (not very likely on a Unix box),
  66. the problem is usually in your code, which uses an unjustified amount of
  67. automatic data.  Before declaring an automatic array, ask yourself whether
  68. that array does indeed need to be automatic.  If the answer is "no", 
  69. declare it as static.  If the stack overflow was caused by recursion,
  70. then it is very likely that your algorithm is not suitable to a recursive
  71. approach.  If you _really_ have to increase your stack, ask your sysadmin
  72. how to do it (probably using a shell built-in command, a la "limit").
  73.  
  74. Dan
  75. --
  76. Dan Pop
  77. CERN, CN Division
  78. Email: danpop@mail.cern.ch 
  79. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  80.